import Page from '@/components/Page';
import Docs from '@/container/Docs';

import req from '@/utils/req';
import { GetServerSideProps, NextPage } from 'next';

const Index: NextPage<ResponseDocs & { body: string }> = ({ files, body }) => {
  return (
    <Page>
      <Docs files={files} body={body} />
    </Page>
  );
};

export default Index;

export const getServerSideProps: GetServerSideProps = async (ctx) => {
  const res = await req.get<ResponseDocs>('/v/docs');
  const filePath = ctx.params?.id as string;

  let body = '';
  try {
    const res = await req.get<{ body: string }>(`/v/doc/${filePath}`);
    body = res.body;
  } catch (error) {
    body = error.msg || '404';
  }

  return {
    props: {
      files: res.files,
      body,
    },
  };
};
